﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace MediaBrowser.CustomControls.Transitionals
{
    /// <summary>
    /// Logique d'interaction pour FadeAndBlurTransition.xaml
    /// </summary>
    public partial class FadeTransition : UserControl
    {
        private ContentPresenter newContentPresenter;
        private bool isTransitioning;

        public FadeTransition()
        {
            InitializeComponent();
            newContentPresenter = new ContentPresenter();
        }

        public object ContentTransition
        {
            get { return (object)GetValue(ContentTransitionProperty); }
            set { SetValue(ContentTransitionProperty, value); }
        }
        public static readonly DependencyProperty ContentTransitionProperty =
            DependencyProperty.Register("ContentTransition",
                typeof(object),
                typeof(FadeTransition),
                new UIPropertyMetadata(null, OnContentTransitionChanged));

        private static void OnContentTransitionChanged(object element, DependencyPropertyChangedEventArgs e)
        {
            FadeTransition te = element as FadeTransition;
            if (te != null)
            {
                te.BeginTransition();
            }
        }

        private void BeginTransition()
        {
            if (ContentTransition != null)
            {
                if (!isTransitioning)
                {
                    isTransitioning = true;
                    if (CtCenter.Content != null)
                    {
                        //oldContentPresenter
                        ScaleTransform scaleOld = new ScaleTransform();
                        CtCenter.RenderTransform = scaleOld;
                        newContentPresenter.RenderTransformOrigin = new Point(0.5, 0.5);

                        //newContentPresenter
                        ScaleTransform scale = new ScaleTransform();
                        newContentPresenter.RenderTransform = scale;
                        newContentPresenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        newContentPresenter.Content = ContentTransition;
                        ((UIElement)ContentTransition).Opacity = 0.5;
                        scale.ScaleX = 0.9;
                        scale.ScaleY = 0.9;
                        GrdMain.Children.Add(newContentPresenter);

                        //Animation OldContent
                        DoubleAnimation oldScaleXContentAnimation = new DoubleAnimation(0.9, new Duration(TimeSpan.FromMilliseconds(500)));
                        oldScaleXContentAnimation.AccelerationRatio = 0.5;
                        oldScaleXContentAnimation.DecelerationRatio = 0.5;
                        scaleOld.BeginAnimation(ScaleTransform.ScaleXProperty, oldScaleXContentAnimation);

                        DoubleAnimation oldScaleYContentAnimation = new DoubleAnimation(0.9, new Duration(TimeSpan.FromMilliseconds(500)));
                        oldScaleYContentAnimation.AccelerationRatio = 0.5;
                        oldScaleYContentAnimation.DecelerationRatio = 0.5;
                        scaleOld.BeginAnimation(ScaleTransform.ScaleYProperty, oldScaleYContentAnimation);

                        DoubleAnimation oldOpacityContentAnimation = new DoubleAnimation(0.5, new Duration(TimeSpan.FromMilliseconds(700)));
                        oldOpacityContentAnimation.AccelerationRatio = 0.5;
                        oldOpacityContentAnimation.DecelerationRatio = 0.5;
                        ((UIElement)CtCenter.Content).BeginAnimation(ContentPresenter.OpacityProperty, oldOpacityContentAnimation);

                        //Animation NewContent
                        DoubleAnimation newScaleXContentAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(700)));
                        newScaleXContentAnimation.AccelerationRatio = 0.5;
                        newScaleXContentAnimation.DecelerationRatio = 0.5;
                        scale.BeginAnimation(ScaleTransform.ScaleXProperty, newScaleXContentAnimation);

                        DoubleAnimation newScaleYContentAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(700)));
                        newScaleYContentAnimation.AccelerationRatio = 0.5;
                        newScaleYContentAnimation.DecelerationRatio = 0.5;
                        scale.BeginAnimation(ScaleTransform.ScaleYProperty, newScaleYContentAnimation);

                        DoubleAnimation newOpacityContentAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(700)));
                        newOpacityContentAnimation.AccelerationRatio = 0.5;
                        newOpacityContentAnimation.DecelerationRatio = 0.5;
                        newOpacityContentAnimation.Completed += lastAnimation_Completed;
                        ((UIElement)ContentTransition).BeginAnimation(ContentPresenter.OpacityProperty, newOpacityContentAnimation);
                    }
                    else
                    {
                        ScaleTransform scale = new ScaleTransform();
                        CtCenter.RenderTransform = scale;
                        CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        scale.ScaleX = 0.9;
                        scale.ScaleY = 0.9;

                        CtCenter.Content = ContentTransition;

                        DoubleAnimation newScaleXContentAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(500)));
                        newScaleXContentAnimation.AccelerationRatio = 0.5;
                        newScaleXContentAnimation.DecelerationRatio = 0.5;
                        scale.BeginAnimation(ScaleTransform.ScaleXProperty, newScaleXContentAnimation);

                        DoubleAnimation newScaleYContentAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(500)));
                        newScaleYContentAnimation.AccelerationRatio = 0.5;
                        newScaleYContentAnimation.DecelerationRatio = 0.5;
                        newScaleYContentAnimation.Completed += firstAnimation_Completed;
                        scale.BeginAnimation(ScaleTransform.ScaleYProperty, newScaleYContentAnimation);
                    }
                }
            }
        }

        void firstAnimation_Completed(object sender, EventArgs e)
        {
            isTransitioning = false;
        }

        void lastAnimation_Completed(object sender, EventArgs e)
        {
            CtCenter.ClearValue(ContentPresenter.RenderTransformProperty);
            CtCenter.Content = ContentTransition;
            GrdMain.Children.Remove(newContentPresenter);
            isTransitioning = false;
        }

    }
}
